<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */
 
class CartController extends Zenddn_Controller_Action{
	
    public function init(){
		parent::init();
        parent::loadTemplate('magashop');
        $this->view->page = "home";
	}
	
	public function indexAction(){
	    parent::addScripts(array('jquery-price-format.min.js'));
        $request = $this->_request;
        
        $data = array('count' => 0, 'total' => 0, 'items' => array());
        
        $cart = new Zend_Session_Namespace('cart');
        
        $haveCoupon = false;
        
        $data['subtotal'] = 0;
        
        if(isset($cart->count) && $cart->count > 0){
            $data['items'] = get_object_vars($cart->items);
            $data['count'] = $cart->count;
            $data['total'] = $cart->total;
            $data['subtotal'] = $cart->subtotal;
        }
        
        $nscoupon = new Zend_Session_Namespace('coupon');
        
        if(isset($nscoupon->haveCoupon) && $nscoupon->haveCoupon){
            $haveCoupon = $nscoupon->haveCoupon;
            
            $coupon['voucher_code'] = $nscoupon->voucher_code;
            $coupon['description'] = $nscoupon->description;
            $coupon['order_value'] = $nscoupon->order_value;
            $coupon['type'] = $nscoupon->type;
            $coupon['value'] = $nscoupon->value;
            $coupon['discount_price'] = 0;
            
            if($data['total'] >= $coupon['order_value']){
                if(!$nscoupon->type){
                    $discount_price = $nscoupon->value;
                }else{
                    $discount_price = ($data['total'] * $nscoupon->value) / 100;
                }
                
                $data['total'] -= $discount_price;
                
                if($data['total'] < 0){
                    $data['total'] = 0;
                }
                
                $coupon['discount_price'] = $discount_price;
            }
            
            $this->view->coupon = $coupon;
        }
        
        $this->view->haveCoupon = $haveCoupon;
        $this->view->data = $data;
        
        if($request->isPost()){
            $param = $request->getParam('quantity');
            
            $ns = new Zend_Session_Namespace('cart');
            $ns->unlock();
            
            foreach($param as $id => $quantity){
                if($quantity == 0){
                    unset($ns->items->{$id});
                }else{
                    $ns->items->{$id}['quantity'] = $quantity;
                
                    $product = $ns->items->{$id};
                    $product['total'] = $quantity * $product['price'];
                    $ns->items->{$id} = $product;
                }
            }
            
            $count = 0;
            $total = 0;
            
            foreach($ns->items as $row){
                $ns->items->{$id}['total'] = $row['quantity'] * $row['price'];
                $total += $row['quantity'] * $row['price'];
                $count++;
            }
            
            $ns->total = $total;
            $ns->subtotal = $total;
            $ns->count = $count;
            
            $ns->lock();
            
            $this->_helper->_redirector->gotoRoute(array(), 'cart', true);
        }
	}
    
    public function addAction(){
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout()->disableLayout();
        
        $request = $this->_request;
        
        $data['error'] = false;
        $data['message'] = '';
        
        if($request->isXmlHttpRequest()){
            $id = $request->getParam('id');
            $quantity = $request->getParam('quantity');
            
            $model = new Default_Model_Product();
            $product = $model->getData($id, array('id', 'name', 'quantity', 'price', 'image'));
            
            if($product && $product['quantity'] > 0){
                if($product['quantity'] >= $quantity){
                    $product['quantity'] = $quantity;
                    $product['total'] = $quantity * $product['price'];
                    
                    $ns = new Zend_Session_Namespace('cart');
                    $ns->unlock();
                    
                    $ns->items->{$id} = $product;
                    
                    $count = 0;
                    $total = 0;
                    
                    foreach($ns->items as $row){
                        $total += $row['quantity'] * $row['price'];
                        $count++;
                    }
                    
                    $ns->total = $total;
                    $ns->count = $count;
                    $ns->subtotal = $total;
                    
                    $ns->lock();
                }else{
                    $data['error'] = true;
                    $data['message'] = 'not enought product';
                }
            }else{
                $data['error'] = true;
                $data['message'] = 'out of stock';
            }
            
            
            header('Content-type: application/json');
            echo json_encode($data);
        }
    }
    
    public function loadAction(){
        $this->_helper->layout()->disableLayout();
        
        $request = $this->_request;
        $data = array('count' => 0, 'total' => 0, 'items' => array());
        
        if($request->isXmlHttpRequest()){
            $cart = new Zend_Session_Namespace('cart');
            
            if(isset($cart->count) && $cart->count > 0){
                $data['items'] = get_object_vars($cart->items);
                $data['count'] = $cart->count;
                $data['total'] = $cart->total;
            }
            
            $this->view->data = $data;
        }
    }
    
    public function deleteAction(){
    	$this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout()->disableLayout();
        
        $request = $this->_request;
        
        $data['error'] = true;
        
        if($request->isXmlHttpRequest()){
            $id = $request->getParam('id');
            
            $ns = new Zend_Session_Namespace('cart');
            $ns->unlock();
            
            unset($ns->items->{$id});
            
            $count = 0;
            $total = 0;
            
            foreach($ns->items as $row){
                $total += $row['quantity'] * $row['price'];
                $count++;
            }
            
            $ns->total = $total;
            $ns->count = $count;
            $ns->subtotal = $total;
            
            $ns->lock();
            
            $data['error'] = false;
            
            header('Content-type: application/json');
            echo json_encode($data);
        }
    }
}